package no.progark.helikopter;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

public class AnimatedSprite {
	
	private Bitmap mAnimation; // Bitmap containing the animation
    private int mXPos; // x-cord for the sprite
    private int mYPos; // y-cord for the sprite
    private Rect mSRectangle; // Rectangle which covers the rendereing part of the screen
    private int mFPS; // frames shown per second
    private int mNoOfFrames; // number of frames in the sprite
    private int mCurrentFrame; // the frame which is rendered currently
    private long mFrameTimer; // 
    private int mSpriteHeight; // height of an individual frame
    private int mSpriteWidth; // width of an individual frame  
    
    public int getXPos(){
    	return mXPos;
    }
    
    public int getYPos(){
    	return mYPos;
    }

    public AnimatedSprite(){
        mSRectangle = new Rect(0,0,0,0);
        mFrameTimer =0;
        mCurrentFrame =0;
        mXPos = 80;
        mYPos = 200;
    }
    
    public void Initalise(Bitmap theBitmap, int Height, int Width, int theFPS, int theFrameCount) {
        mAnimation = theBitmap;
        mSpriteHeight = Height;
        mSpriteWidth = Width;
        mSRectangle.top = 0;
        mSRectangle.bottom = mSpriteHeight;
        mSRectangle.left = 0;
        mSRectangle.right = mSpriteWidth;
        mFPS = 1000 /theFPS;
        mNoOfFrames = theFrameCount;
    }
    
    public void Update(long GameTime) {
        if(GameTime > mFrameTimer + mFPS ) {
            mFrameTimer = GameTime;
            mCurrentFrame +=1;
     
            if(mCurrentFrame >= mNoOfFrames) {
                mCurrentFrame = 0;
            }
        }
     
        mSRectangle.left = mCurrentFrame * mSpriteWidth;
        mSRectangle.right = mSRectangle.left + mSpriteWidth;
    }
    
    public void draw(Canvas canvas) {
        Rect dest = new Rect(getXPos(), getYPos(), getXPos() + mSpriteWidth,
                        getYPos() + mSpriteHeight);
     
        canvas.drawBitmap(mAnimation, mSRectangle, dest, null);
    }
    
    
}
